/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Scroll Parallax. 
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import util.FpsLabel;
import graphics.ParallaxManager;

import aze.display.TileLayer;
import aze.display.SparrowTilesheet;
import nme.Assets;
import nme.display.Sprite;
import nme.events.Event;
import nme.Lib;

class Parallax extends Sprite {

	var fpstext:FpsLabel;                  // Muestra los FPS
	var parallaxManager:ParallaxManager;   // Manager del Efecto

	var layer:TileLayer;                   // Capa general
	var previousTime:Int;                  // Para calcular el delta

	// Constructor =====================================================
	public function new() {
		super(); haxe.Timer.delay(init, 150);
	}

	function init():Void {
		createScene();
		addListeners();
		previousTime = Lib.getTimer();
	}

	// Carga de Recursos ===============================================
	function loadAssets():Void{
		var sheetData:String = Assets.getText("assets/bee.xml");
		var tilesheet:SparrowTilesheet = new SparrowTilesheet(Assets.getBitmapData("assets/parallax_background_layer.png"), sheetData);
		layer = new TileLayer(tilesheet);
		addChild(layer.view);
	}

	// Creación de la Escena ===========================================
	function createScene():Void {
		loadAssets();
		fpstext = new FpsLabel(20, 20, 0x606060, "assets/cafeta.ttf");  
		addChild(fpstext);
		parallaxManager = new ParallaxManager(layer);
		parallaxManager.addSky("sky");
		parallaxManager.addBackgroundStrip("humo", 4, 10, 475, 15);
		parallaxManager.addBackgroundStrip("cactus", 4, 10, 215, 30);
	}

	// Bucle Principal =================================================
	function mainLoop(eTime:Int):Void {
		fpstext.update();
		parallaxManager.update(stage.stageWidth, stage.stageHeight, eTime);
		layer.render();
	}

	// Listeners =======================================================
	function addListeners():Void {
		stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}

	function onEnterFrame(event:Event):Void {
		var now = Lib.getTimer();
		var elapsedTime = now - previousTime;
		previousTime = now;
		mainLoop(elapsedTime);
	}

	// Main ============================================================  
	static public function main() {
		Lib.current.addChild(new Parallax());
	}
}
